package com.iotechn.unimall.admin.api.company;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.component.CacheComponent;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.data.constant.CacheConst;
import com.iotechn.unimall.data.domain.AdminDO;
import com.iotechn.unimall.data.domain.CompanyDO;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.CompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.Date;
import java.util.List;

@Service
public class AdminCompanyServiceImpl implements AdminCompanyService {

    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private CacheComponent cacheComponent;

    /**
     * 创建
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(String companyName, String companyAddress, Long adminId) throws ServiceException {
        Date now = new Date();
        CompanyDO companyDO = new CompanyDO();
        companyDO.setCompanyName(companyName);
        companyDO.setCompanyAddress(companyAddress);
        companyDO.setGmtCreate(now);
        companyDO.setGmtUpdate(now);
        if (companyMapper.insert(companyDO) > 0) {
            this.clearCache(companyName);
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.COMPANY_SQL_ADD_FAILED);
    }

    /**
     * 删除
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (companyMapper.delete(new QueryWrapper<CompanyDO>()
                .eq("id", id)) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.COMPANY_SQL_DELETE_FAILED);
    }

    /**
     * 编辑
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(Long id,String companyName, String companyAddress, Long adminId) throws ServiceException {
        CompanyDO companyDO = new CompanyDO();
        companyDO.setId(id);
        companyDO.setCompanyName(companyName);
        companyDO.setCompanyAddress(companyAddress);
        companyDO.setGmtUpdate(new Date());
        if (companyMapper.updateById(companyDO) > 0) {
            this.clearCache(companyName);
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.COMPANY_SQL_UPDATE_FAILED);
    }

    /**
     * 列表
     * @return
     * @throws ServiceException
     */
    @Override
    public Page<CompanyDO> list(String name, Integer page, Integer limit, Long adminId) throws ServiceException {
        QueryWrapper<CompanyDO> wrapper = new QueryWrapper<>();
        if (name != null) {
            wrapper.like("company_name", name);
        }
        return companyMapper.selectPage(Page.div(page, limit, CompanyDO.class), wrapper);
    }

    /**
     * 下拉列表
     * @return
     * @throws ServiceException
     */
    @Override
    public List<CompanyDO> xialist(Long adminId) throws ServiceException {
        List<CompanyDO> companyDOS = companyMapper.selectList(
                new QueryWrapper<CompanyDO>());
        return companyDOS;
    }

    public void clearCache(String companyName) {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                cacheComponent.del(CacheConst.COMPANY_CACHE + companyName);
            }
        });
    }
}
